package com.skt.controller;

import com.skt.domain.VO.FilePublicUrlVO;
import com.skt.domain.VO.FileUploadVO;
import com.skt.service.FileService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.Positive;

@RestController
@Tag(name = "文件接口")
@RequiredArgsConstructor
@RequestMapping("/files")
public class FileController {

    private final FileService fileService;

    @Operation(summary = "上传图片")
    @PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    // (@RequestParam("file") MultipartFile file): 从请求中获取名为 "file" 的上传文件。
    public ResponseEntity<FileUploadVO> uploadImage(@RequestParam("file") MultipartFile file) {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        FileUploadVO result = fileService.uploadImage(file, securityContext);
        return ResponseEntity.ok(result);
    }

    @Operation(summary = "获取文件的公开访问 URL")
    @GetMapping("/{fileId}/public-url")
    public ResponseEntity<FilePublicUrlVO> getPublicFileUrl(@PathVariable Long fileId) {
        FilePublicUrlVO vo = fileService.getPublicFileUrl(fileId);
        if (vo == null) {
            return ResponseEntity.notFound().build(); // 现在安全了
        }
        return ResponseEntity.ok(vo);
    }
}